package com.slwish.common.mvp.presenter;

import android.app.Dialog;
import android.content.Context;

import com.slwish.common.dialog.PictureDialog;
import com.slwish.common.mvp.contract.BaseContract;
import com.zhouyou.http.subsciber.IProgressDialog;

/**
 * @author Rocky
 * @version 1.0.0
 */
public class BasePresenter<T extends BaseContract.View> implements BaseContract.Presenter {
    private T mView;

    protected IProgressDialog showProgressDialog(final Context context) {
        IProgressDialog mProgressDialog = new IProgressDialog() {
            @Override
            public Dialog getDialog() {
                return new PictureDialog(context);
            }
        };
        return mProgressDialog;
    }

    public BasePresenter(T view) {
        setView(view);
    }

    /**
     * 设置一个View，子类可以复写
     */
    @SuppressWarnings("unchecked")
    protected void setView(T view) {
        this.mView = view;
        this.mView.setPresenter(this);
    }

    /**
     * 给子类使用的获取View的操作
     * 不允许复写
     *
     * @return View
     */
    protected final T getView() {
        return mView;
    }

    @Override
    public void start() {
        // 开始的时候进行Loading调用
        T view = mView;
        if (view != null) {
            view.showLoading();
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void destroy() {
        T view = mView;
        mView = null;
        if (view != null) {
            // 把Presenter设置为NULL
            view.setPresenter(null);
        }
    }

    public void setError(String msg) {
        if (mView != null)
            mView.showError(msg);
    }

    public void setError(int msg) {
        if (mView != null)
            mView.showError(msg);
    }
}
